package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entiey.Spu;
import com.leyou.item.entiey.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {

    @Autowired
    private SpecParamService specParamService;
    @Resource
    private SpuService spuService;

    //根据spuId查询spu的所有规格参数值
    @Override
    public List<SpecParamDTO> querySpecParamById(Long id, Boolean searching) {

        // 1.规格参数value
        SpuDetail spuDetail = this.getById(id);
        //查到的结果为json类型，将json转化为map
        Map<Long, Object> specValues = JsonUtils
                .toMap(spuDetail.getSpecification(), Long.class, Object.class);

        //2.规格参数对象
        //2.1 通过spuId 查询tb_spu表中信息
        Spu spu = spuService.getById(id);
        //2.2 调用 查询规格参数集合  得到符合条件的规格参数对象
        List<SpecParamDTO> ParamList = specParamService.querySpecParams(spu.getCid3(),null, searching);
        //2.3 遍历规格参数，将参数value注入对应的规格参数
        for (SpecParamDTO Param : ParamList) {
            Param.setValue(specValues.get(Param.getId()));
        }

        return ParamList;

    }
}